package com.blb.community_admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.blb.community_admin.entity.ZyRepair;
import com.blb.community_admin.entity.ZyVisitor;
import com.blb.community_admin.entity.vo.VisitorVO;
import com.blb.community_admin.service.IZyVisitorService;
import com.blb.community_admin.util.EasyExcelUtil;
import com.blb.community_admin.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "访客模块")
@RestController
public class ZyVisitorController {

    @Autowired
    private IZyVisitorService zyVisitorService;

    /**
     * 访客列表
     *
     * @param
     * @return
     */
    @ApiOperation("分页模糊查询")
    @PostMapping("/visitor/page")
    public ResponseResult<IPage<ZyVisitor>> selectCommunityPage(@RequestBody ZyVisitor zyVisitor) {
        IPage<ZyVisitor> page = zyVisitorService.selectVisitorPage(zyVisitor.getCurrent(), zyVisitor.getSize(), zyVisitor);
        return ResponseResult.ok(page);
    }


    /**
     * 添加访客信息
     *
     * @param zyVisitor
     * @return
     */
    @ApiOperation("添加访客")
    @PostMapping("/visitor")
    public ResponseResult<String> addZyVisitor(@RequestBody ZyVisitor zyVisitor) {
        boolean save = zyVisitorService.save(zyVisitor);
        System.out.println(save);
        zyVisitor.setCreateTime(LocalDateTime.now());
        return ResponseResult.ok("添加成功");
    }

    /**
     * 修改
     *
     * @param zyVisitor
     * @return
     */
    @ApiOperation("修改")
    @PutMapping("/visitor")
    public ResponseResult<String> updateZyVisitor(@RequestBody(required = false) ZyVisitor zyVisitor) {
        zyVisitor.setUpdateTime(LocalDateTime.now());
        zyVisitorService.updateById(zyVisitor);
        return ResponseResult.ok("修改成功");
    }

    /**
     * 删除
     *
     * @param visitorid
     * @return
     */
    @ApiOperation("删除")
    @DeleteMapping("visitor/{visitorid}")
    public ResponseResult<String> deleteZyVisitor(@PathVariable Long visitorid) {
        zyVisitorService.removeById(visitorid);
        return ResponseResult.ok("删除成功");
    }


    @ApiOperation("导出访客记录")
    @PostMapping("visitor/download")
    public ResponseEntity<ByteArrayResource> simpleWritea(@RequestBody ArrayList<String> visitorid, HttpServletResponse response) {
        ResponseEntity<ByteArrayResource> download = null;
        try {
            List<ZyVisitor> zyVisitors = null;
            if (visitorid.isEmpty()) {
                zyVisitors = zyVisitorService.list(null);
            } else {
                zyVisitors = zyVisitorService.selectById(visitorid);
            }

            String path = EasyExcelUtil.write("访客记录数据", ZyVisitor.class, zyVisitors);
            download = EasyExcelUtil.download(path);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return download;


    }
}
